home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / connx-1.001 / connx-1~ / commun.c < prev    next >
C/C++ Source or Header  |  1995-02-11  |  6KB  |  323 lines

  1. /*
  2.  * socket package - supports both unix scoket files
  3.  * and TCPIP sockets. Use -DTCPIP when compiling if
  4.  * TCPIP sockets are desired.
  5.  */
  6.  
  7. #include "commun.h"
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netdb.h>
  13. #include <sys/signal.h>
  14. #include <sys/uio.h>
  15. #include <sys/time.h>
  16.  
  17. #ifdef AIX
  18. #include <sys/select.h>
  19. #endif
  20.  
  21. #ifdef TCPIP
  22. #include <netinet/in.h>
  23. #include <arpa/inet.h>
  24. #else
  25. #include <sys/un.h>
  26. #endif
  27.  
  28. /****************************************************************************/
  29. #ifdef DEBUG
  30. #define PRINTF(x) printf(x)
  31. #else
  32. #define PRINTF(x) ;
  33. #endif
  34.  
  35. /*
  36.  * open server socket to be used to accept
  37.  * client connections
  38.  */
  39. int
  40. server_open_socket (int *sockfd)
  41. {
  42.  
  43.  
  44.   /* setup socket address */
  45. #ifdef TCPIP
  46.   struct sockaddr_in src;
  47.   src.sin_addr.s_addr = INADDR_ANY;
  48.   src.sin_port = SOCK_PORT;
  49. #else
  50.   struct sockaddr_un src;
  51.   unlink (SOCK_FILE);
  52.   src.sun_family = AF_UNIX;
  53.   strcpy (src.sun_path, SOCK_FILE);
  54. #endif
  55.  
  56.   /* broken pipes are ignored. */
  57.   signal (SIGPIPE, SIG_IGN);
  58.  
  59.   if ((*sockfd = socket (
  60. #ifdef TCPIP
  61.               AF_INET,
  62. #else
  63.               AF_UNIX,
  64. #endif
  65.               SOCK_STREAM, 0)) < 0)
  66.     {
  67.       printf ("socket failure errno=%d\n", errno);
  68.       return -1;
  69.     }
  70.  
  71.  
  72.   if (bind (*sockfd, (struct sockaddr *) &src, sizeof (src)) < 0)
  73.     {
  74.       printf ("bind failure errno=%d\n", errno);
  75.       return -1;
  76.     }
  77.  
  78.   if (listen (*sockfd, MAX_CONN) < 0)
  79.     {
  80.       printf ("listen failure errno=%d\n", errno);
  81.       return -1;
  82.     }
  83.  
  84.   return 0;
  85.  
  86. }
  87.  
  88.  
  89. /****************************************************************************/
  90.  
  91. /*
  92.  * server accepts connections from client
  93.  */
  94. int
  95. server_accept_connection (int sockfd, msg_conn * conns)
  96. {
  97.   int fromlen;
  98.   int new_sock;
  99.  
  100.   /* socket address */
  101. #ifdef TCPIP
  102.   struct sockaddr_in fromend;
  103. #else
  104.   struct sockaddr_un fromend;
  105. #endif
  106.  
  107.   fromlen = sizeof fromend;
  108.  
  109.   if (conns->number_of_conn == MAX_CONN)
  110.     {
  111.       printf ("maximum number of connections\n");
  112.       return -1;
  113.     }
  114.  
  115.   new_sock = accept (sockfd, (struct sockaddr *) &fromend, &fromlen);
  116.   if (new_sock != -1)
  117.     {
  118.       conns->conns[conns->number_of_conn++] = new_sock;
  119.       return 0;
  120.     }
  121.   else
  122.     return -1;
  123. }
  124.  
  125.  
  126. /****************************************************************************/
  127.  
  128. /*
  129.  * client opens connection with server
  130.  */
  131. int
  132. client_open_connection (int *sockfd, char *name)
  133. {
  134.  
  135.   /* use name to find TCPIP address of server */
  136. #ifdef TCPIP
  137.   struct sockaddr_in src;
  138.   struct hostent *ph;
  139.   int len;
  140.   long address;
  141.  
  142.   if (isdigit (name[0]))
  143.     {
  144.       if ((address = inet_addr (name)) == -1)
  145.     {
  146.       printf ("invalid host name\n");
  147.       return -1;
  148.     }
  149.       src.sin_addr.s_addr = address;
  150.       src.sin_family = AF_INET;
  151.     }
  152.   else if ((ph = gethostbyname (name)) == NULL)
  153.     {
  154.       printf ("invalid host name\n");
  155.       return -1;
  156.     }
  157.   else
  158.     {
  159.       src.sin_family = ph->h_addrtype;
  160.       bcopy (ph->h_addr, (char *) &src.sin_addr, ph->h_length);
  161.     }
  162.  
  163.   src.sin_port = SOCK_PORT;
  164.  
  165. #else
  166.  
  167.   /* use socket file to communicate with server */
  168.   struct sockaddr_un src;
  169.   src.sun_family = AF_UNIX;
  170.   strcpy (src.sun_path, SOCK_FILE);
  171.  
  172. #endif
  173.  
  174.   /* ignore broken pipes */
  175.   signal (SIGPIPE, SIG_IGN);
  176.  
  177.   if ((*sockfd = socket (
  178. #ifdef TCPIP
  179.               AF_INET,
  180. #else
  181.               AF_UNIX,
  182. #endif
  183.               SOCK_STREAM, 0)) < 0)
  184.     {
  185.       printf ("socket errno=%d\n", errno);
  186.       return -1;
  187.     }
  188.  
  189.   if (connect (*sockfd, (struct sockaddr *) &src, sizeof (src)) < 0)
  190.     {
  191.       printf ("connect errno=%d\n", errno);
  192.       return -1;
  193.     }
  194.   return 0;
  195. }
  196.  
  197.  
  198. /****************************************************************************/
  199.  
  200. /*
  201.  * send a message across a socket
  202.  */
  203. int
  204. send_message (int *sock, int type, int size, char *msg)
  205. {
  206.   struct iovec vec[3];
  207.  
  208.   if (*sock == -1)
  209.     return -1;
  210.  
  211.   vec[0].iov_base = (char *) &type;
  212.   vec[0].iov_len = sizeof (int);
  213.   vec[1].iov_base = (char *) &size;
  214.   vec[1].iov_len = sizeof (int);
  215.   vec[2].iov_base = msg;
  216.   vec[2].iov_len = size;
  217.  
  218.   if (writev (*sock, vec, 3) != (size + (2 * sizeof (int))))
  219.     {
  220.       PRINTF ("close on send\n");
  221.       close_socket (sock);
  222.       return -1;
  223.     }
  224.  
  225.   return 0;
  226. }
  227.  
  228.  
  229. /****************************************************************************/
  230.  
  231.  
  232. /*
  233.  * read a message from a socket
  234.  */
  235. int
  236. read_message (int *sock, gen_msg * msg_receive)
  237. {
  238.   int typ, len, rc;
  239.  
  240.   if (*sock == -1)
  241.     return -1;
  242.  
  243.   /* read message type */
  244.   rc = read (*sock, &typ, sizeof (int));
  245.   if (rc != sizeof (int))
  246.     {
  247.       PRINTF ("close on read\n");
  248.       close_socket (sock);
  249.       return -1;
  250.     }
  251.  
  252.   /* read message length */
  253.   if (read (*sock, &len, sizeof (int)) != sizeof (int))
  254.     {
  255.       PRINTF ("close on read\n");
  256.       close_socket (sock);
  257.       return -1;
  258.     }
  259.  
  260.   msg_receive->msg_type = typ;
  261.   msg_receive->msg_length = len;
  262.  
  263.   /* read message data */
  264.   if (len > 0)
  265.     if (read (*sock, msg_receive->msg, len) != len)
  266.       {
  267.     PRINTF ("close on read\n");
  268.     close_socket (sock);
  269.     return -1;
  270.       }
  271.  
  272.   return 0;
  273. }
  274.  
  275. /*****************************************************************************/
  276.  
  277. /*
  278.  * check to see if a socket has closed (sent EOF)
  279.  */
  280. int
  281. check_status (int *sock)
  282. {
  283.   int rc, buf;
  284.   struct fd_set readfs;
  285.   struct timeval notimeout;
  286.  
  287.   if (*sock == -1)
  288.     return -1;
  289.  
  290.   notimeout.tv_sec = 0;
  291.   notimeout.tv_usec = 0;
  292.   FD_ZERO (&readfs);
  293.   FD_SET (*sock, &readfs);
  294.  
  295.   if ((rc = select ((*sock) + 1, &readfs, NULL, NULL, ¬imeout)) != 0)
  296.     {
  297.       rc = recv (*sock, (char *) &buf, sizeof (int), MSG_PEEK);
  298.       if (rc <= 0)        /* socket closed */
  299.     {
  300.       close_socket (sock);
  301.       return -1;
  302.     }
  303.     }
  304.   return 0;
  305. }
  306.  
  307. /****************************************************************************/
  308.  
  309. /*
  310.  * close a socket
  311.  */
  312. int
  313. close_socket (int *sock)
  314. {
  315.   if (*sock != -1)
  316.     {
  317.       close (*sock);
  318.       *sock = -1;
  319.     }
  320. }
  321.  
  322. /****************************************************************************/
  323.